package com.itbuka.vip.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.itbuka.vip.domain.VipPointsHistory;
import com.itbuka.vip.service.VipPointsHistoryService;
import com.itbuka.vip.mapper.VipPointsHistoryMapper;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;


import java.util.List;

/**
* @author ld
* @description 针对表【vip_points_history】的数据库操作Service实现
* @createDate 2024-07-29 16:24:09
*/
@Service
public class VipPointsHistoryServiceImpl implements VipPointsHistoryService{
    @Autowired
    private VipPointsHistoryMapper vipPointsHistoryMapper;

    @Override
    public List<VipPointsHistory> selectAll() {
        LambdaQueryWrapper <VipPointsHistory> lqw = new LambdaQueryWrapper<>();
        lqw.eq(VipPointsHistory::getIsDelete,0);
        return vipPointsHistoryMapper.selectList(lqw);
    }

    @Override
    public List<VipPointsHistory> selectList(VipPointsHistory vipPointsHistory) {
        LambdaQueryWrapper <VipPointsHistory> lqw = this.lqw(vipPointsHistory);
        return vipPointsHistoryMapper.selectList(lqw);
    }

    @Override
    public Integer insert(VipPointsHistory vipPointsHistory) {
        return vipPointsHistoryMapper.insert(vipPointsHistory);
    }

    @Override
    @Transactional
    public Integer delete(String ids) {
        try {
            String[] split = ids.split(",");
            for (String id : split) {
                VipPointsHistory vipPointsHistory = new VipPointsHistory();
                vipPointsHistory.setId(Long.parseLong(id));
                vipPointsHistory.setIsDelete(1);
                vipPointsHistoryMapper.updateById(vipPointsHistory);
            }
            return 1;
        }catch (Exception e){
            return -1;
        }
    }

    @Override
    public Integer update(VipPointsHistory vipPointsHistory) {
        return vipPointsHistoryMapper.updateById(vipPointsHistory);
    }

    @Override
    public Page<VipPointsHistory> pageAll(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        return (Page<VipPointsHistory>)this.selectAll();
    }

    @Override
    public Page<VipPointsHistory> pageList(VipPointsHistory vipPointsHistory, Integer page, Integer size) {
        PageHelper.startPage(page,size);
        return (Page<VipPointsHistory>)this.selectList(vipPointsHistory);
    }

    public LambdaQueryWrapper<VipPointsHistory> lqw(VipPointsHistory vipPointsHistory){
        LambdaQueryWrapper <VipPointsHistory> lqw = new LambdaQueryWrapper<>();
        if (vipPointsHistory.getId() != null){
            lqw.eq(VipPointsHistory::getId,vipPointsHistory.getId());
        }
        if (vipPointsHistory.getVipName() != null){
            lqw.eq(VipPointsHistory::getVipName,vipPointsHistory.getVipName());
        }
        if (vipPointsHistory.getOperationContent() != null){
            lqw.eq(VipPointsHistory::getOperationContent,vipPointsHistory.getOperationContent());
        }
        if (vipPointsHistory.getPreviousPoints() != null){
            lqw.eq(VipPointsHistory::getPreviousPoints,vipPointsHistory.getPreviousPoints());
        }
        if (vipPointsHistory.getVariationCredit() != null){
            lqw.eq(VipPointsHistory::getVariationCredit,vipPointsHistory.getVariationCredit());
        }
        if (vipPointsHistory.getCurrentPoint() != null){
            lqw.eq(VipPointsHistory::getCurrentPoint,vipPointsHistory.getCurrentPoint());
        }
        if (vipPointsHistory.getOperatingTime() != null){
            lqw.eq(VipPointsHistory::getOperatingTime,vipPointsHistory.getOperatingTime());
        }
        if (vipPointsHistory.getIsDelete() != null){
            lqw.eq(VipPointsHistory::getIsDelete,vipPointsHistory.getIsDelete());
        }
        if (vipPointsHistory.getCreateTime() != null){
            lqw.eq(VipPointsHistory::getCreateTime,vipPointsHistory.getCreateTime());
        }
        if (vipPointsHistory.getUpdateTime() != null){
            lqw.eq(VipPointsHistory::getUpdateTime,vipPointsHistory.getUpdateTime());
        }
        return lqw;
    }
}




